//#include "stdafx.h"
#import "ios_lib.h"
#import "SRManager.h"
//#import <time.h>
////#define CALLBACK  __stdcall
//struct timer_attr{
//	bool bUsed;
//	app_timer_id_t timer_id;
//	app_timer_mode_t mode;
//	app_timer_timeout_handler_t timeout_handler;
//	void * p_context;
//};
//
//timer_attr all_timer[10];
//typedef void* HWND;
//typedef unsigned short UINT;
//typedef unsigned long DWORD;
//void TimerProc(
//						HWND hwnd, 
//						UINT uMsg, 
//						UINT idEvent, 
//						DWORD dwTime 
//						)
//{
//	BOOL bFind = FALSE;
//	int id;
//	for (id = 0; id < 10; id++)
//	{
//		if (all_timer[id].timer_id == idEvent)
//		{
//			bFind = TRUE;
//			break;
//		}
//	}
//
//	if (!bFind)
//	{
//		return;
//	}
//
//	if (all_timer[id].mode == APP_TIMER_MODE_SINGLE_SHOT)
//	{
//        [paintingTimer invalidate];
//		//KillTimer(NULL, all_timer[id].timer_id);
//	}
//
//	all_timer[id].timeout_handler(all_timer[id].p_context);
//}


//uint32_t app_timer_create(app_timer_id_t *            p_timer_id,
//						  app_timer_mode_t            mode,
//						  app_timer_timeout_handler_t timeout_handler)
//{
//	int i;
//	for (i = 0; i < 10; i++)
//	{
//		if (!all_timer[i].bUsed)
//		{
//			break;
//		}
//	}
//
//	all_timer[i].bUsed = TRUE;
//	all_timer[i].mode = mode;
//	all_timer[i].timeout_handler = timeout_handler;
//	all_timer[i].p_context = NULL;
//
//	*p_timer_id = i;
//
//	return NRF_SUCCESS;
//}

//uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
//{
//    
//    paintingTimer = [NSTimer scheduledTimerWithTimeInterval:timeout_ticks
//                                                     target:nil
//                                                   selector:@selector(TimerProc:)
//                                                   userInfo:nil
//                                                    repeats:YES];
//	//all_timer[timer_id].timer_id = SetTimer(NULL, timer_id, timeout_ticks, TimerProc);
//    all_timer[timer_id].timer_id =paintingTimer;
//	all_timer[timer_id].p_context = p_context;
//	return NRF_SUCCESS;
//}
//
//uint32_t app_timer_stop(app_timer_id_t timer_id)
//{
//    [paintingTimer invalidate];
//	//KillTimer(NULL, all_timer[timer_id].timer_id);
//	return NRF_SUCCESS;
//}

//uint32_t app_query_timer(app_timer_id_t timer_id)
//{
//	return NRF_SUCCESS;
//}

// ble_nus.c

static SendCompletePara m_send_complete_para =  {NULL, NULL,TASK_NONE};

void set_complete_callback(SendCompletePara para)
{
	m_send_complete_para.callback = para.callback;
	m_send_complete_para.context = para.context;
	m_send_complete_para.task_type = para.task_type;
}

//BOOL serial_send(uint8_t * str, uint16_t *length);
uint32_t ble_nus_send_string(uint8_t * str, uint16_t *length, BLECallBack callback)
{
    static const char * UUID = "\x6E\x40\x00\x02\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E";
    NSData * uuidData = [NSData dataWithBytes:UUID length:16];
    CBUUID * uuid = [CBUUID UUIDWithData:uuidData];
    
    NSData * data = [NSData dataWithBytes:str length:*length];
    NSLog(@"SEND DATA TO DEVICE:  %@", data);
    
    [[SRManager sharedInstance] updateCharacteristicUUID:uuid
                                               withValue:data
                                              completion:^(id data) {
                                                  if (data == nil) {
                                                      NSLog(@"BlueTooth TimeOut");
                                                  }
                                                  else
                                                  {
                                                      NSLog(@"RECEIVED REPONSE...");
                                                  }
                                                  bluetooth_l0_reset();
                                                  if (callback != NULL) {
                                                      callback(data == NULL);
                                                  }
                                              }];
	return NRF_SUCCESS;
}

/************************************************************************/
/*                                                                      */
/*           windows func end                                           */
/*                                                                      */
/************************************************************************/